package com.lightkits.wms.domain.deliveryorder;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsDeliveryOrderBaseMapper;
import com.lightkits.wms.database.model.BdDeliveryTaskModel;
import com.lightkits.wms.dto.deliveryorder.QueryDeliveryByDoRequestType;
import com.lightkits.wms.dto.deliveryorder.QueryDeliveryByDoResponseType;
import com.lightkits.wms.dto.deliveryorder.model.DeliverTaskDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * QueryDeliveryByDoExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/28 10:53
 */
@Component
public class QueryDeliveryByDoExecutor implements ServiceExecutor<QueryDeliveryByDoRequestType, QueryDeliveryByDoResponseType> {
    private final WmsDeliveryOrderBaseMapper bdDeliveryOrderBaseMapper;

    private final Logger LOGGER = LoggerFactory.getLogger(QueryDeliveryByDoExecutor.class);

    @Autowired
    public QueryDeliveryByDoExecutor(WmsDeliveryOrderBaseMapper bdDeliveryOrderBaseMapper) {
        this.bdDeliveryOrderBaseMapper = bdDeliveryOrderBaseMapper;
    }

    @Override
    public QueryDeliveryByDoResponseType service(QueryDeliveryByDoRequestType request) {
        QueryDeliveryByDoResponseType response = new QueryDeliveryByDoResponseType();
        response.setSuccessful(true);
        List<BdDeliveryTaskModel> models =
                bdDeliveryOrderBaseMapper.queryDeliveryTaskByDoNum(request.getDoNumOrId(), SessionInfo.languageCode());
        if (ObjectUtil.isEmpty(models)) {
            return response;
        }
        List<DeliverTaskDTO> taskDTOS = new ArrayList<>();
        models.forEach(target -> {
            DeliverTaskDTO dto = this.convertDeliveryTask(target);
            taskDTOS.add(dto);
        });
        response.setDeliverTaskDTO(taskDTOS);
        return response;
    }

    private DeliverTaskDTO convertDeliveryTask(BdDeliveryTaskModel taskModel) {
        DeliverTaskDTO dto = new DeliverTaskDTO();
        try {
            dto.setDoId(taskModel.getDoId());
            dto.setDoNumber(taskModel.getDoNumber());
            dto.setMakeOrderQty(taskModel.getMakeOrderQty());
            dto.setCustomerItem(taskModel.getCustomerItem());
            dto.setCustomerId(taskModel.getCustomerId());
            dto.setCustomerName(taskModel.getCustomerName());
            dto.setPrimaryUom(taskModel.getPrimaryUom());
            dto.setPrimaryUomText(taskModel.getPrimaryUomText());
            dto.setItemDesc(taskModel.getItemDesc());
            dto.setItemCode(taskModel.getItemCode());
            dto.setOrderStatusText(taskModel.getOrderStatusText());
            dto.setOrderStatusCode(taskModel.getOrderStatusCode());
            dto.setItemId(taskModel.getItemId());
            dto.setOrgId(taskModel.getOrgId());
            dto.setDeliveryQty(taskModel.getDeliveryQty());
            dto.setDemandQty(taskModel.getDemandQty());
            if (!ObjectUtil.isEmpty(taskModel.getDemandDate())) {
                dto.setDemandDate(DateUtil.toDateString(taskModel.getDemandDate(), DateUtil.DEFAULT_DATE_PATTERN));
            }
        } catch (Exception e) {
            LOGGER.error("getTaskDto.error", e);
            throw new LightKitsBizException(e);
        }
        return dto;
    }
}
